{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Alternating Digit Sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: alternateDigitSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #交替数字和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个正整数 <code>n</code> 。<code>n</code> 中的每一位数字都会按下述规则分配一个符号：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><strong>最高有效位</strong> 上的数字分配到 <strong>正</strong> 号。</li>\n",
    "\t<li>剩余每位上数字的符号都与其相邻数字相反。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回所有数字及其对应符号的和。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 521\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>(+5) + (-2) + (+1) = 4</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 111\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>(+1) + (-1) + (+1) = 1\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 886996\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>(+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [alternating-digit-sum](https://leetcode.cn/problems/alternating-digit-sum/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [alternating-digit-sum](https://leetcode.cn/problems/alternating-digit-sum/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['521', '111', '886996']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        nums_str = str(n)\n",
    "        spilt_list = []\n",
    "        for i in range(len(nums_str)):\n",
    "            spilt_list.append(((-1) ** i) * int(nums_str[i]))\n",
    "\n",
    "        return sum(spilt_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        n=str(n)\n",
    "        ans=0\n",
    "        k=1\n",
    "        for i in n:\n",
    "            ans=ans+k*int(i)\n",
    "            k=k*(-1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        return sum((-1) ** i * int(x) for i, x in enumerate(str(n)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        n = str(n)\n",
    "        sum = 0\n",
    "        \n",
    "        for i in range(len(n)):\n",
    "            if i%2==0:\n",
    "                sum+=int(n[i])\n",
    "                \n",
    "            if i%2!=0:\n",
    "                sum-=int(n[i])\n",
    "                \n",
    "        return sum;  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        ans, sign = 0, 1\n",
    "        while n:\n",
    "            ans += n% 10 * sign\n",
    "            sign = -sign\n",
    "            n //=10\n",
    "        return ans * -sign\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        res = 0\n",
    "        flag = -1\n",
    "        while n > 0:\n",
    "            flag *= -1\n",
    "            res += flag * (n % 10)\n",
    "            n //= 10\n",
    "        return res * flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        l = len(str(n))\n",
    "        s = str(n)\n",
    "        x = y = 0\n",
    "        for i in range(0,l,2):\n",
    "            x += int(s[i])\n",
    "        for j in range(1,l,2):\n",
    "            y -= int(s[j])\n",
    "        return x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        list1=[]\n",
    "        ans=0\n",
    "        # 先把每个位的数取出来\n",
    "        while(n):\n",
    "            a=n%10  # 余数\n",
    "            list1.append(a)\n",
    "            n=n//10\n",
    "        print(list1)\n",
    "        n=len(list1)\n",
    "        for i in range(n):\n",
    "        # i 取0,1,2,3,,,n-1\n",
    "            i=n-1-i\n",
    "            # i 取n-1,n-2,,,0\n",
    "            # 判断n-1的正负\n",
    "            if n%2==0 : # n-1是奇数\n",
    "                ans+=(-1)**(i+1)*list1[i]\n",
    "            else:\n",
    "                ans+=(-1)**i*list1[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "      return sum((-1) ** i*int(x) for i,x in enumerate(str(n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        s=0\n",
    "        g=str(n)\n",
    "        for i in range(len(g)):\n",
    "            if i%2==0:s+=int(g[i])\n",
    "            else:s-=int(g[i])\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "      lst_n = [x for x in str(n)]\n",
    "      sum = 0\n",
    "      for idx in range(len(lst_n)):\n",
    "          if idx % 2 == 0:\n",
    "              sum += int(lst_n[idx])\n",
    "          elif idx % 2 == 1:\n",
    "              sum -= int(lst_n[idx])\n",
    "      return sum\n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        t=str(n)\n",
    "        s=0\n",
    "        for i in range(len(t)):\n",
    "            if i%2==0:\n",
    "                s+=int(t[i])\n",
    "            else:\n",
    "                s-=int(t[i])\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        ret = 0\n",
    "        flag = 1\n",
    "        while n > 0:\n",
    "            ret = ret + (n % 10) * flag \n",
    "            n = n // 10\n",
    "            flag = -flag\n",
    "        return -flag * ret\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        f = 1\n",
    "        while n:\n",
    "            ans += n % 10 * f\n",
    "            n //= 10\n",
    "            f *= -1\n",
    "        if f == 1:\n",
    "            return -ans\n",
    "        return ans\n",
    "\n",
    "        # s = str(n)\n",
    "        # for i in s:\n",
    "        #     ans += f * int(i)\n",
    "        #     f *= -1\n",
    "        # return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        return sum((-1) ** i * int(x) for i, x in enumerate(str(n)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        sum=0\n",
    "        for i in range(len(str(n))):\n",
    "            if i%2==0:\n",
    "                sum+=int(str(n)[i])\n",
    "            else:\n",
    "                sum-=int(str(n)[i])\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        sum = 0\n",
    "        nstr = str(n)\n",
    "        for i in range(len(nstr)):\n",
    "            if i % 2 == 0:\n",
    "                sum += int(nstr[i])\n",
    "            else:\n",
    "                sum -= int(nstr[i])\n",
    "        return sum  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        n_str = str(n)\n",
    "        result = 0\n",
    "        sign = 1\n",
    "        for digit in n_str:\n",
    "            num = int(digit)\n",
    "            result += sign * num\n",
    "            sign *= -1\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        return (lambda x,y:x-y)(*list(map(lambda x:sum(x), [list(map(lambda x:int(x), str(n)))[_::2] for _ in range(2)])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "\n",
    "        ans = 0\n",
    "        sign = 1\n",
    "        while n > 0:\n",
    "            ans += sign * (n % 10)\n",
    "            n //= 10\n",
    "            sign *= -1\n",
    "        \n",
    "\n",
    "        ans *= (sign*-1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        for i,num in enumerate(str(n)):\n",
    "            if i%2==0:\n",
    "                ans+=int(num)\n",
    "            else: \n",
    "                ans-=int(num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        list_num = list(map(int,str(n)))\n",
    "        for i in range(len(list_num)):\n",
    "            if i % 2 == 1:\n",
    "                list_num[i] = -list_num[i]\n",
    "        return sum(list_num)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        return sum(int(c) * (-1) ** i for i, c in enumerate(str(n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        re = 0\n",
    "        for i,x in enumerate(str(n)):\n",
    "            if i % 2 == 0:\n",
    "                re += int(x)\n",
    "            else:\n",
    "                re -= int(x) \n",
    "        return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        # s = str(n)\n",
    "        # t,ans = 1,0\n",
    "        \n",
    "        # for i in s:\n",
    "        #     ans += int(i)*t\n",
    "        #     t *= -1\n",
    "        return sum(int(x)*(-1)**i for i,x in enumerate(str(n)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        s = str(n)\n",
    "        a1 = sum(map(int, s[::2]))\n",
    "        a2 = sum(map(int, s[1::2]))\n",
    "        return a1-a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        #将数字转化成字符串，然后给出符号后相加\n",
    "        nums=str(n)\n",
    "        flag,ans=1,0\n",
    "        for i in nums:\n",
    "            ans+=int(i)*flag\n",
    "            flag=-flag\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        l = list(str(n))\n",
    "        s = 0\n",
    "        flag = 1\n",
    "        for i in l:\n",
    "            s += int(i)*flag\n",
    "            flag *= -1\n",
    "        return s\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        n=str(n)\n",
    "        s=0\n",
    "        for i in range(len(n)):\n",
    "            if i%2==0:\n",
    "                s+=int(n[i])\n",
    "            else:\n",
    "                s-=int(n[i])\n",
    "\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        return sum((-1)**i*int(x) for i,x in enumerate(str(n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        res,fuhao= 0,1\n",
    "        for i in str(n):\n",
    "            res += int(i)*fuhao\n",
    "            fuhao*=-1\n",
    "        return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        res = 0\n",
    "        flag = 1\n",
    "        for s in str(n):\n",
    "            res += flag*int(s)\n",
    "            flag = -flag\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        numbers = []\n",
    "        while(n!=0):\n",
    "            numbers.append(n%10)\n",
    "            n = n//10\n",
    "        numbers.reverse()\n",
    "        switch = True\n",
    "        sum = 0\n",
    "        for a in numbers:\n",
    "            if switch:\n",
    "                sum+=a\n",
    "            else:\n",
    "                sum-=a\n",
    "            switch=not switch\n",
    "\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        ans,flag = 0,1\n",
    "        for i in str(n):\n",
    "            ans += flag*int(i)\n",
    "            flag *= -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        n = list(str(n))\n",
    "        result = 0\n",
    "        for i in range(len(n)):\n",
    "            if i&1 == 0:\n",
    "                result += int(n[i])\n",
    "            else: result -= int(n[i])\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        temp_list = self.get_n_list(n)[::-1]\n",
    "        return_sum = 0\n",
    "        for i in range(len(temp_list)):\n",
    "            if i % 2 == 0:\n",
    "                return_sum += temp_list[i]\n",
    "            else:\n",
    "                return_sum -= temp_list[i]\n",
    "        return return_sum \n",
    "\n",
    "    def get_n_list(self, n):\n",
    "        return_list = []\n",
    "        len_n = len(str(n))\n",
    "        num_n = n\n",
    "        for _ in range(len_n):\n",
    "            return_list.append(num_n % 10)\n",
    "            num_n = int(num_n / 10)\n",
    "        return return_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        for i, x in enumerate(str(n)):\n",
    "            ans += int(x) * (-1) ** i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        res = 0\n",
    "        Falg = 1\n",
    "        for i in map(int, str(n)):\n",
    "            res = res + i if Falg else res -i \n",
    "            Falg ^= 1\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "      return sum((-1) ** i*int(x) for i,x in enumerate(str(n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "\n",
    "        res = 0\n",
    "        num_str = str(n)\n",
    "\n",
    "        for i in range(len(num_str)):\n",
    "            if i % 2 == 0:\n",
    "                res += int(num_str[i])\n",
    "            else:\n",
    "                res -= int(num_str[i])\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        numbers = []\n",
    "        while(n!=0):\n",
    "            numbers.append(n%10)\n",
    "            n = n//10\n",
    "        numbers.reverse()\n",
    "        switch = True\n",
    "        sum = 0\n",
    "        for a in numbers:\n",
    "            if switch:\n",
    "                sum+=a\n",
    "            else:\n",
    "                sum-=a\n",
    "            switch=not switch\n",
    "\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        s=str(n)\n",
    "        res=0\n",
    "        for i in range(len(s)):\n",
    "            res+=int(s[i])*((-1)**i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        flag = 0\n",
    "        ans = 0\n",
    "        while n:\n",
    "            flag ^= 1\n",
    "            a = n%10\n",
    "            if flag==1:\n",
    "                ans += a\n",
    "            else:\n",
    "                ans -= a\n",
    "            n //= 10\n",
    "            \n",
    "        if flag==1:\n",
    "            return ans\n",
    "        else:\n",
    "            return -ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "\n",
    "        res, sign = 0, 1\n",
    "\n",
    "        while n > 0:\n",
    "            res += n % 10 * sign\n",
    "            sign = -sign\n",
    "            n //= 10\n",
    "        \n",
    "        \n",
    "        return -sign * res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        ans, sign=0, 1\n",
    "        for c in str(n):\n",
    "           x= int(c)\n",
    "           ans +=sign*x\n",
    "           sign *=-1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        sum=0\n",
    "        num_str = str(n)\n",
    "        for i in range(len(num_str)):\n",
    "            sum += int(num_str[i])*pow(-1,i)\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        # ans, sign=0, 1\n",
    "        # for c in str(n):\n",
    "        #    x= int(c)\n",
    "        #    ans +=sign*x\n",
    "        #    sign *=-1\n",
    "        # return ans\n",
    "        # return sum((-1) ** i * int(x) for i, x in enumerate(str(n)))\n",
    "        lst = [int(x) for x in str(n)]\n",
    "        return sum(lst[::2]) - sum(lst[1::2])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        v=0\n",
    "        for i in range(len(str(n))):\n",
    "            if i%2==0:\n",
    "                v+=abs(int(str(n)[i]))\n",
    "            else:\n",
    "                v+=-abs(int(str(n)[i]))\n",
    "        return v\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        sum = 0\n",
    "        num = -1 \n",
    "        nlist = list(str(n))\n",
    "        for i in range(len(nlist)):\n",
    "            sum += int(nlist[i]) * (-1) ** i\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        sign=1\n",
    "        s=0\n",
    "        for c in str(n)[::1]:\n",
    "            s+=int(c)*sign\n",
    "            sign*=-1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        res, sign = 0, 1\n",
    "        while n:\n",
    "            res += n % 10 * sign\n",
    "            sign = -sign\n",
    "            n //= 10\n",
    "        return -sign * res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        numbers = []\n",
    "        while(n!=0):\n",
    "            numbers.append(n%10)\n",
    "            n = n//10\n",
    "        numbers.reverse()\n",
    "        switch = True\n",
    "        sum = 0\n",
    "        for a in numbers:\n",
    "            if switch:\n",
    "                sum+=a\n",
    "            else:\n",
    "                sum-=a\n",
    "            switch=not switch\n",
    "\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        ans = idx = 0\n",
    "        s = str(n)\n",
    "        length = len(s)\n",
    "        while idx < length:\n",
    "            temp = int(s[idx])\n",
    "            if idx % 2 != 0:\n",
    "                temp = -temp\n",
    "            ans += temp\n",
    "            idx += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        numbers = []\n",
    "        while(n!=0):\n",
    "            numbers.append(n%10)\n",
    "            n = n//10\n",
    "        numbers.reverse()\n",
    "        switch = True\n",
    "        sum = 0\n",
    "        for a in numbers:\n",
    "            if switch:\n",
    "              sum+=a\n",
    "            else:\n",
    "                sum-=a\n",
    "            switch=not switch\n",
    "        return sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        n_s = str(n)\n",
    "        sign = 1\n",
    "        res = 0\n",
    "        for s in n_s:\n",
    "            res += sign*int(s)\n",
    "            sign *= -1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        s = str(n)\n",
    "        num = 0\n",
    "        for i in range(len(s)):\n",
    "            if i % 2 == 0:\n",
    "                num += int(s[i])\n",
    "            else:\n",
    "                num -= int(s[i])\n",
    "        return num\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        ans, f = 0, 1\n",
    "        while n:\n",
    "            ans += n % 10 * f\n",
    "            n //= 10\n",
    "            f *= -1\n",
    "        return -f * ans\n",
    "\n",
    "        # s = str(n)\n",
    "        # for i in s:\n",
    "        #     ans += f * int(i)\n",
    "        #     f *= -1\n",
    "        # return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        strn = str(n)\n",
    "        result = 0\n",
    "        for i in range(0,len(strn)):\n",
    "            if i % 2 == 0:\n",
    "                result += int(strn[i])\n",
    "            else:\n",
    "                result -= int(strn[i])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        result=0\n",
    "        nums=[]\n",
    "        while n!=0:\n",
    "            nums.append(n%10)\n",
    "            n=n//10\n",
    "        for i in range(0,len(nums)):\n",
    "            tmp=nums[-i-1]\n",
    "            if (i&1)!=0:\n",
    "                tmp=-tmp\n",
    "            result+=tmp\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        s = str(n)\n",
    "        flag = 1\n",
    "        ans = 0\n",
    "        for ch in s:\n",
    "            ans += int(ch) * flag\n",
    "            flag *= -1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        res = 0\n",
    "        flag = 1\n",
    "        for s in str(n):\n",
    "            res += flag*int(s)\n",
    "            flag = -flag\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        res,sign = 0,1\n",
    "        while n:\n",
    "            res += n%10*sign\n",
    "            sign = -sign\n",
    "            n //=10\n",
    "        return -sign*res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        strs = str(n)\n",
    "        res = 0\n",
    "        for i in range(len(strs)):\n",
    "            res += int(strs[i]) * (-1) ** i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n>0:\n",
    "            res = n % 10 - res\n",
    "            n //= 10\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        return sum([int(str(n)[i])*-1 if i%2 else int(str(n)[i])  for i in range(len(str(n)))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        # 时间 logn\n",
    "        # 空间 1\n",
    "        res, sign = 0, 1\n",
    "        while n:\n",
    "            res += n % 10 * sign\n",
    "            sign = -sign\n",
    "            n //= 10\n",
    "        return -sign * res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        res = 0\n",
    "        s = [int(x) for x in str(n)]\n",
    "        for i in range(len(s)):\n",
    "            d = (-1) ** i * s[i]\n",
    "            res = res + d\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        lst = list(map(int,str(n)))\n",
    "        ans = 0\n",
    "        for i in range(len(lst)):\n",
    "            c = 1 if i % 2 == 0 else -1\n",
    "            ans += c * lst[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        n = str(n)\n",
    "        answer = 0\n",
    "        for k in range(len(n)):\n",
    "            if k%2 == 0:\n",
    "                answer += int(n[k])\n",
    "            else:\n",
    "                answer -= int(n[k])\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        s = str(n)\n",
    "        return sum([int(s[i])*-1 if i % 2 else int(s[i]) for i in range(len(s))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        return sum((-1) ** i * int(x) for i, x in enumerate(str(n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternateDigitSum(self, n: int) -> int:\n",
    "        return sum([int(i) for i in str(n)[::2]]) + sum([0-int(i) for i in str(n)[1::2]])\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
